package util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Base64;

public class ImgToBase64 {
	public static String fileToBase64(File file) {
		FileInputStream fis = null;
		ByteArrayOutputStream bos = null;
		String code = "";
		try {
			fis = new FileInputStream(file);
			bos = new ByteArrayOutputStream();
			int len = 0;
			byte[] b = new byte[1024];
			while ((len = fis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			byte[] pic = bos.toByteArray();
			code = Base64.getEncoder().encodeToString(pic);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return code;

	}

	public static void Base64Tofile(String photo,String pathname) {
		File file = new File(pathname);
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			byte[] photoBytes = photo.getBytes();
			byte[] codeBytes = Base64.getDecoder().decode(photoBytes);
			fos.write(codeBytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
